import React from "react";
import Home from "@/components/home";
import Login from "@/components/business/login";

// 引入当前目录下面的所有的js文件
const ModulesFile = import.meta.glob('./*.tsx', { eager: true })

// 路由集合
const List: any[] = []

//提取每个对象下的值 同步获取
Object.values(ModulesFile).map((item: any) => {
    if (item.default) {
        List.push(...item.default)
    }
})

// 定义一个名为RouterList的函数组件
const RouterList = () => {

    //将用户的登录信息记录到状态数据中
    let cookie = React.Cookie.load('Business') ? React.Cookie.load('Business') : {};
    let [Business, SetBusiness] = React.useState(cookie)

    const isEmpty = (obj: Object) => {
        return Object.keys(obj).length === 0;
    };

    // 定义全局使用
    React.Business = Business;
    React.SetBusiness = SetBusiness

    //生成一个路由的全局跳转方法
    const navigate = React.Router.useNavigate()
    React.navigate = navigate

    //构建一个路由守卫的子组件
    const AuthRouter = (props: any) => {
        //判断当前路由是否有auth的属性
        if (props.auth) {
            //需要判断登录 没有属性，说明对象是空，没有登录
            if (Object.getOwnPropertyNames(cookie).length <= 0) {
                return <React.Router.Navigate to="/business/login" replace={true} />
            }

            let id = cookie.hasOwnProperty('id') ? cookie.id : 0;
            let mobile = cookie.hasOwnProperty('mobile') ? cookie.mobile : '';

            let result: any = React.HTTP.post('/business/check', {
                id: id, mobile: mobile
            });

            result.then((res: any) => {
                if (res.code == 0) {
                    React.Cookie.remove('Business');
                    return (
                        <React.Router.Navigate to="/business/login" />
                    )
                } else {
                    if (res.data) {
                        //刷新覆盖一下cookie
                        React.Cookie.save('Business', res.data);
                        React.Business = res.data;
                    }

                    return (
                        <>
                            {props.component}
                        </>
                    )
                }
            })

            return (
                <>
                    {props.component}
                </>
            )
        } else {
            //不需要判断,直接显示当前要访问的组件
            return (
                <>
                    {props.component}
                </>
            )
        }
    }

    // 返回一个包含Routes和Route的组件
    return (
        <>
            <React.Router.Routes>
                <React.Router.Route path="/" element={<Home />} />
                <React.Router.Route path="/" element={<><React.Router.Outlet /></>}>
                    {
                        List.map((item: any, index: number) => {
                            return (
                                <React.Router.Route key={index} path={item.path} element={<AuthRouter auth={item.auth ? item.auth : false} component={<item.component/>}></AuthRouter>}>
                                    {
                                        item.children && item.children.map((child: any, ids: number) => {
                                            if (isEmpty(cookie) && child.auth) {
                                                return (
                                                    <React.Router.Route key={ids} path={child.path} element={<Login />}></React.Router.Route>
                                                )
                                            }
                                            return (
                                                <React.Router.Route key={ids} path={child.path} element={<AuthRouter auth={child.auth ? child.auth : false} component={<child.component />}></AuthRouter>}></React.Router.Route>
                                            )
                                        })
                                    }
                                </React.Router.Route>
                            )
                        })
                    }
                </React.Router.Route>
            </React.Router.Routes>
        </>
    )
}


export default RouterList;